package com.jishuo.nontax.util;

import com.jishuo.base.service.SequenceService;
import com.jishuo.nontax.bean.message.*;
import com.jishuo.nontax.bean.vo.*;
import com.jishuo.nontax.bfc.dao.FiscMapper;
import com.jishuo.nontax.bfc.service.BaseDataService;
import com.jishuo.nontax.bfc.service.UnitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 凭证库所需数据处理
 * Author: jingcaiqi1225@gmail.com
 * Date: 2024/4/7 11:00
 */
@Component
@Slf4j
public class PzkUtils {

    @Autowired
    private UnitService unitService;

    @Autowired
    private BaseDataService baseDataService;

    @Autowired
    private FiscMapper fiscMapper;

    @Autowired
    private SequenceService sequenceService;

    /**
     * 一般缴款书电子票柜面收款
     * @param bill 请求数据
     * @return 所属数据map
     */
    public Map<String, Object> getChargeBill(ChargeBillVO bill) {
        BankVO bankVO = baseDataService.selectBankById(Math.toIntExact(bill.getBankid()));

        ArrayList<ChargeBillDetailVO> chargeBillDetailVOS = bill.getDetailVOS();
        ArrayList<SetChargeBillDetailDTO> list = new ArrayList<>();
        for (ChargeBillDetailVO vo : chargeBillDetailVOS) {
            SetChargeBillDetailDTO dto = new SetChargeBillDetailDTO();
            dto.setItemcode(vo.getItemcode());
            dto.setItemname(vo.getItemname());
            dto.setOriginfund(String.valueOf(vo.getOriginfund()));
            dto.setAmount(String.valueOf(vo.getAmount()));
            dto.setLowerLimit(vo.getLowerLimit());
            dto.setUpperLimit(vo.getUpperLimit());
            dto.setIsLimit(vo.getIsLimit());
            list.add(dto);
        }

        HashMap<String, Object> obj = new HashMap<>();
        obj.put("billNum", bill.getBillnum());
        obj.put("cantonId", bill.getCantonid());
        obj.put("hallId", bill.getHallid());
        obj.put("fileDate", bill.getFilldate());
        obj.put("payer", bill.getPayer());
        obj.put("payerAccount", bill.getPayeraccount());
        obj.put("payerBank", bill.getPayerbank());
        obj.put("bankId", bill.getBankid());
        obj.put("verifyCode", bill.getVertifycode());
        obj.put("originFund", bill.getOriginfund());
        obj.put("unitCode", bill.getUnitcode());
        obj.put("unitName", bill.getUnitname());
        obj.put("bankStation", bill.getBankstation());
        obj.put("payBankDt", bill.getPaybankdt());
        obj.put("accountName", bankVO.getAccountname());
        obj.put("accountCode", bankVO.getAccountcode());
        obj.put("bankName", bankVO.getBankname());
        obj.put("chargebilldetail", list);

        log.info("缴款书电子票打印：" + obj);
        return obj;
    }

    /**
     * 一般缴款书手工票柜面收款
     * @param bill 请求数据
     * @return 返回数据map
     */
    public Map<String, Object> getChargeBillManual(SetChargeBillManual bill) {

        if (!bill.getChargebilldetail().isEmpty()) {
            for (SetChargeBillManualDetailDTO dto : bill.getChargebilldetail()) {
                dto.setLowerLimit("10.00");
                dto.setUpperLimit("1.00");
                dto.setIsLimit(1);
            }
        }


        // 查询凭证库所需银行信息
        BankVO bankVO = baseDataService.selectBankById(Integer.parseInt(bill.getBankid()));
        // 查询凭证库所需单位信息
        UnitVO unitVO = unitService.selectUnitById(bill.getUnitid());

        HashMap<String, Object> obj = new HashMap<>();
        obj.put("billNum", bill.getBillnum());
        obj.put("cantonId", bill.getCantonid());
        obj.put("hallId", bill.getHallid());
        obj.put("fillDate", bill.getFilldate());
        obj.put("payer", bill.getPayer());
        obj.put("payerAccount", bill.getPayeraccount());
        obj.put("payerBank", bill.getPayerbank());
        obj.put("bankId", bill.getBankid());
        obj.put("verifyCode", bill.getVertifycode());
        obj.put("originFund", bill.getOriginfund());
        obj.put("payBankDt", bill.getPaybankdt());
        obj.put("bankStation", bill.getBankstation());
        obj.put("unitCode", unitVO.getUnitcode());
        obj.put("unitName", unitVO.getUnitname());
        obj.put("accountName", bankVO.getAccountname());
        obj.put("accountCode", bankVO.getAccountcode());
        obj.put("bankName", bankVO.getBankname());
        obj.put("chargeBillDetail", bill.getChargebilldetail());

        log.info("缴款书手工票打印：" + obj);
        return obj;
    }

    /**
     * 退付书
     * @param bill 请求数据
     * @return 返回数据map
     */
    public Map<String, Object> getBackBill(BackpaybillVO bill) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("backBillNum", bill.getBackbillnum());
        obj.put("cantonId", bill.getCantonid());
        obj.put("drawMan", bill.getDrawman());
        obj.put("drawBank", bill.getDrawbank());
        obj.put("drawAccount", bill.getDrawaccount());
        obj.put("bankId", bill.getBankid());
        obj.put("fund", bill.getFund());
        obj.put("accountName", bill.getAccountname());
        obj.put("accountCode", bill.getAccountcode());
        obj.put("bankName", bill.getBankname());
        obj.put("itemCode", bill.getItemcode());
        obj.put("itemName", bill.getItemname());
        obj.put("dealDate", bill.getDealdate());
        obj.put("bankStation", bill.getBankstation());

        log.info("退付书打印" + obj);
        return obj;
    }

    /**
     * 获取凭证库待查所需信息
     * @param waitaffirm 待查对象
     * @return map数据
     */
    public Object getWaitAffirm(SetWaitaffirm waitaffirm) {
        ArrayList<SetWaitaffirmDTO> dtos = waitaffirm.getWaitaffirm();
        for (SetWaitaffirmDTO dto : dtos) {
            BankVO bankVO = baseDataService.selectBankById(Integer.parseInt(dto.getBankid()));
            dto.setAccountCode(bankVO.getAccountcode());
            dto.setBankName(bankVO.getBankname());
            dto.setAccountName(bankVO.getAccountname());
            dto.setCantonName((baseDataService.selectCantonById(Integer.parseInt(dto.getCantonid()))).getCantonname());
        }
        SetWaitaffirm setWaitaffirm = new SetWaitaffirm();
        setWaitaffirm.setWaitaffirm(dtos);
        log.info("打印：" + setWaitaffirm);
        return setWaitaffirm;
    }

    public Object getBankBalance(List<BankBalanceVO> list) {
        SetBankBalancePzk balance = new SetBankBalancePzk();
        ArrayList<BankBalanceDetail> bankbalance = new ArrayList<>();
        for (BankBalanceVO vo : list) {
            BankBalanceDetail dto = new BankBalanceDetail();
            dto.setAccountDate(vo.getAccountdate());
            dto.setAccountName(vo.getAccountname());
            dto.setAccountCode(vo.getAccountcode());
            dto.setBankId(vo.getBankid());
            dto.setBankName(vo.getBankname());
            dto.setFund(String.valueOf(vo.getFund()));
            bankbalance.add(dto);
        }
        balance.setBankbalance(bankbalance);
        log.info("打印：" + bankbalance);
        return balance;
    }

    /**
     * 获取学生信息
     * @param vo 学生数据
     * @return 对象
     */
    public Object getStudent(StudentVO vo) {
        SetStudentPZK setStudent = new SetStudentPZK();
        setStudent.setCantonId(String.valueOf(vo.getCantonid()));
        setStudent.setStudentNum(vo.getStudentnum());
        setStudent.setClassName(vo.getClassname());
        setStudent.setStudentName(vo.getStudentname());
        setStudent.setBankId(String.valueOf(vo.getBankid()));
        setStudent.setFund(String.valueOf(vo.getFunds()));
        setStudent.setPayerAccount(vo.getPayeraccount());
        setStudent.setAccountCode(vo.getAccountcode());
        setStudent.setBankName(vo.getBankname());
        setStudent.setBankStation(vo.getBankstation());
        setStudent.setPayBankDt(vo.getPaybankdt());
        log.info("打印：" + setStudent);
        return setStudent;
    }

    /**
     * 获取国库 信息
     * @param fiscInfo guoku 信息
     * @return 对象
     */
    public Object getFsicOne(PayFiscVO fiscInfo) {
        SetPayFiscPZK pzk = new SetPayFiscPZK();
        pzk.setMoney(String.valueOf(fiscInfo.getItemmoney1()));
        BankVO bankVO = baseDataService.selectBankById(Math.toIntExact(fiscInfo.getPayerbankid()));
        pzk.setAccountCode(bankVO.getAccountcode());
        pzk.setBankName(bankVO.getBankname());
        pzk.setAccountName(bankVO.getAccountname());
        pzk.setBillNum(fiscInfo.getBillnum());
        pzk.setItemCode(fiscInfo.getItemcode());
        pzk.setItemName(fiscInfo.getItemname());
        pzk.setCantonId(String.valueOf(fiscInfo.getCantonid()));
        pzk.setBankStation(fiscInfo.getBankstation());
        pzk.setPayBankDt(fiscInfo.getPaybankdt());
        pzk.setPayerBankId(String.valueOf(fiscInfo.getPayerbankid()));
        pzk.setPayeeDepart(fiscInfo.getPayeedepart());
        pzk.setPayeeFisc(fiscInfo.getPayeefisc());
        return pzk;
    }

    /**
     * 获取国库 信息
     * @param billNum 票号
     * @return 对象
     */
    public Object getFsicTwo(String billNum) {
        PayFiscVO fiscInfo = fiscMapper.findFiscBankAffirmBybill(billNum);
        BigDecimal money = fiscInfo.getItemmoney1();
        BankVO bankVO = baseDataService.selectBankById(Math.toIntExact(fiscInfo.getPayid()));
        fiscInfo.setAccountcode(bankVO.getAccountcode());
        fiscInfo.setAccountname(bankVO.getAccountname());
        fiscInfo.setBankname(bankVO.getBankname());
        fiscInfo.setMoney(money);
        return fiscInfo;
    }

    /**
     * 获取专户所需对象
     * @param specialInfo 查询出的专户对象
     * @return 所需的专户对象
     */
    public Object getSpecial(PaySpecialVO specialInfo) {
        SetPaySpecialPzk setPaySpecial = new SetPaySpecialPzk();
        setPaySpecial.setBillNum(specialInfo.getBillnum());
        setPaySpecial.setCantonId(String.valueOf(specialInfo.getCantonid()));
        setPaySpecial.setPayerBankId(String.valueOf(specialInfo.getPayerbankid()));
        setPaySpecial.setPayeeLevel(String.valueOf(specialInfo.getPayeelevel()));
        setPaySpecial.setPayeeUnitName(specialInfo.getPayeeunitname());
        setPaySpecial.setPayeeBankName(specialInfo.getPayeebankname());
        setPaySpecial.setPayeeAccount(specialInfo.getPayeebankaccount());
        setPaySpecial.setMoney(String.valueOf(specialInfo.getItemmoney1()));
        setPaySpecial.setItemCode(specialInfo.getItemcode());
        setPaySpecial.setItemName(specialInfo.getItemname());
        BankVO bankVO = baseDataService.selectBankById(Math.toIntExact(specialInfo.getPayerbankid()));
        setPaySpecial.setAccountCode(bankVO.getAccountcode());
        setPaySpecial.setAccountName(bankVO.getAccountname());
        setPaySpecial.setBankName(bankVO.getBankname());
        setPaySpecial.setBankStation(specialInfo.getBankstation());
        setPaySpecial.setPayBankDt(specialInfo.getPaybankdt());
        return setPaySpecial;
    }

    /**
     * 获取凭证库所需的分成对象分成
     * @param divideInfo 对象
     * @return 所需对象
     */
    public Object getDividePzk(PayDivideVO divideInfo) {
        SetDividePzk dividePzk = new SetDividePzk();
        log.info(String.valueOf(dividePzk));
        dividePzk.setDivideBillNum(divideInfo.getDividebillnum());
        dividePzk.setCantonId(String.valueOf(divideInfo.getCantonid()));
        dividePzk.setItemCode(divideInfo.getItemcode());
        dividePzk.setItemName(divideInfo.getItemname());
        dividePzk.setMoney(String.valueOf(divideInfo.getItemmoney()));
        dividePzk.setBankStation(divideInfo.getBankstation());
        dividePzk.setPayBankDt(divideInfo.getPaybankdt());
        if (divideInfo.getPayerbankid() != null) {
            BankVO bankVO = baseDataService.selectBankById(Math.toIntExact(divideInfo.getPayerbankid()));
            dividePzk.setPayerBankId(String.valueOf(divideInfo.getPayerbankid()));
            dividePzk.setPayerAccount(bankVO.getAccountcode());
            dividePzk.setPayerBank(bankVO.getBankname());
        }
        if (divideInfo.getPayeebankid() != null) {
            BankVO bankVO = baseDataService.selectBankById(Math.toIntExact(divideInfo.getPayeebankid()));
            dividePzk.setPayeeAccount(bankVO.getAccountcode());
            dividePzk.setPayeeBank(bankVO.getBankname());
        }
        if (divideInfo.getUnitid() != null) {
            UnitVO unitVO = unitService.selectUnitById(String.valueOf(divideInfo.getUnitid()));
            dividePzk.setPayeeAgency(unitVO.getUnitname());
        }
        if(divideInfo.getParentunitid() != null) {
            UnitVO unitVO = unitService.selectUnitById(String.valueOf(divideInfo.getParentunitid()));
            dividePzk.setPayerAgency(unitVO.getUnitname());
        }
        return dividePzk;
    }

    /**
     * 获取凭证所需电子票对象
     * @param eticketNum 电子票对象
     * @return 对象
     */
    public Object getEticketNum(EticketNumVO eticketNum) {
        SetEticketNumPzk dto = new SetEticketNumPzk();
        dto.setBillNum(eticketNum.getBillnum());
        dto.setAgencyCode(eticketNum.getAgencycode());
        dto.setAgencyName(eticketNum.getAgencyname());
        dto.setPayer(eticketNum.getPayer());
        dto.setPayerAccount(eticketNum.getPayeraccount());
        dto.setPayerBank(eticketNum.getPayerbank());
        dto.setPayee(eticketNum.getPayee());
        dto.setPayeeAccount(eticketNum.getPayeeaccount());
        dto.setPayeeBank(eticketNum.getPayeebank());
        dto.setVerifyCode(eticketNum.getVertifycode());
        dto.setAmt(eticketNum.getAmt());
        dto.setPayInfo(eticketNum.getPayinfo());
        dto.setBankId(eticketNum.getBankid());
        dto.setFillDate(eticketNum.getFilldate());
        dto.setBankStation(eticketNum.getBankstation());
        dto.setPayBankDt(eticketNum.getPaybankdt());
        return dto;
    }
}
